package com.sgcc.manager.sql.helper.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionException;

import com.sgcc.manager.sql.dao.ISqlDao;
import com.sgcc.manager.sql.helper.ISqlComponent;
/***
 * sql事务
 * @ProjectName：ams-app   
 * @ClassName：SqlComponentImpl   
 * @Description: TODO
 * @author suihongxin 
 * @date 2015-2-10 下午01:25:19 
 * @version V1.0
 */
@Service("sqlComponentImpl")
public class SqlComponentImpl implements ISqlComponent{
	@Autowired
	DataSource dataSource;
	@Autowired
	private ISqlDao SqlDaoImpl;
	
	 public static final Map<String, Object> sessionMap = new HashMap<String, Object>();
	    
	@SuppressWarnings("unchecked")
	public int update(String sql,HttpServletRequest requestClient) {
	 String sessionId = requestClient.getSession().getId();
	  Connection conn =null;
	  Statement stmt = null;  
	   int i=0;
	  try {
		  //从sessionMap中拿出conn
		  Object obj =sessionMap.get(sessionId);
		  if(obj==null){
			  conn=DataSourceUtils.getConnection(dataSource); 
			  conn.setAutoCommit(false);
			  conn.setTransactionIsolation(conn.TRANSACTION_READ_COMMITTED);
			  sessionMap.put(sessionId, conn);
		  }else {
			  conn=(Connection) obj;
		  }
		  stmt=conn.createStatement();
		  i=stmt.executeUpdate(sql);
	} catch (TransactionException e) {
		e.printStackTrace();
		try {
			conn.rollback();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	} catch (SQLException e) {
		e.printStackTrace();
		try {
			conn.rollback();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}  
	return i;
	
}

	public int commit(HttpServletRequest requestClient) {
		String sessionId = requestClient.getSession().getId();
		Object obj =sessionMap.get(sessionId);
		Connection conn =null;
		try {
		  conn=(Connection) obj;
		  conn.commit();
		} catch (CannotGetJdbcConnectionException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}


	public int clear(HttpServletRequest requestClient) {
		String sessionId = requestClient.getSession().getId();
		for(Entry<String, Object> entry:sessionMap.entrySet()){
			Connection conn =null;
			if(!entry.getKey().equalsIgnoreCase(sessionId)){
				try {
					conn = (Connection) entry.getValue();
					conn.rollback();
					sessionMap.remove(entry.getKey());
				} catch (CannotGetJdbcConnectionException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}   
		return 0;
	}


	public int rollback(HttpServletRequest requestClient) {
		String sessionId = requestClient.getSession().getId();
		Object obj =sessionMap.get(sessionId);
		Connection conn =null;
		try {
		  conn=(Connection) obj;
		  conn.rollback();
		} catch (CannotGetJdbcConnectionException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0;
	}
	

}
